﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.Model;

public partial class Manage_Cms_Modelitem : System.Web.UI.Page
{
    protected KeRui.BLL.CmsModel bllCmsModel = new KeRui.BLL.CmsModel();
    protected KeRui.Model.CmsModel modelCmsModel= new CmsModel();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();
        //权限判断
        ManageHelper.CheckAdminPower("system_modelmanage");
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            case "cr":
                ChangeOrderID();
                break;
            default:
                break;
        }
    }

    #region 添加
    private void Add()
    {
        //添加权限
        ManageHelper.CheckAdminPower("system_modeladd");
        modelCmsModel.ModelName = Utils.ChkSQL(PageRequest.GetString("ModelName"));
        if (modelCmsModel.ModelName == "")
        {
            nsb.AddErr("请输入模型名称。");
        }
        modelCmsModel.Description = Utils.ChkSQL(PageRequest.GetString("Description"));
        if (modelCmsModel.Description == "")
        {
            nsb.AddErr("请输入描述。");
        }
        //modelCmsModel.ModelType = PageRequest.GetInt("ModelType", -1);
        //if (modelCmsModel.ModelType == -1)
        //{
        //    nsb.AddErr("请选择模型类别。");
        //}
        modelCmsModel.ItemName = Utils.ChkSQL(PageRequest.GetString("ItemName"));
        if (modelCmsModel.ItemName == "")
        {
            nsb.AddErr("请输入项目名称。");
        }
        modelCmsModel.ItemUnit = Utils.ChkSQL(PageRequest.GetString("ItemUnit"));
        if (modelCmsModel.ItemUnit == "")
        {
            nsb.AddErr("请输入项目单位。");
        }
        modelCmsModel.IsUsing = PageRequest.GetString("IsUsing") == "" ? false : true;
        modelCmsModel.OrderID = PageRequest.GetInt("OrderID",-1);
        if (modelCmsModel.ItemUnit == "")
        {
            nsb.AddErr("请输入项目单位。");
        }
        nsb.ChkErr();
        int retInt = bllCmsModel.Add(modelCmsModel);
        if (retInt == 1)
        {
            nsb.AddErr("已有相同的模型名称。");
        }
        else
        {
            //Response.Redirect("Modelitem.aspx?act=add&id=" + modelCmsModel.ModelID + "&sucmsg=添加成功。");
            ManageHelper.AddLog("system_modeladd", "添加了模型：" + modelCmsModel.ModelName);//日志HYC
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"Modellist.aspx?sucmsg=添加成功。\";</script>");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int ModelID = PageRequest.GetInt("ID", -1);
        if (ModelID == -1)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        modelCmsModel = bllCmsModel.GetModel(ModelID);
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        //修改权限hyc
        ManageHelper.CheckAdminPower("system_modelupdate");
        modelCmsModel.ModelID = PageRequest.GetInt("ModelID",-1);
        modelCmsModel.ModelName = Utils.ChkSQL(PageRequest.GetString("ModelName"));
        if (modelCmsModel.ModelName == "")
        {
            nsb.AddErr("请输入模型名称。");
        }
        modelCmsModel.Description = Utils.ChkSQL(PageRequest.GetString("Description"));
        if (modelCmsModel.Description == "")
        {
            nsb.AddErr("请输入描述。");
        }
        //modelCmsModel.ModelType = PageRequest.GetInt("ModelType", -1);
        //if (modelCmsModel.ModelType == -1)
        //{
        //    nsb.AddErr("请选择模型类别。");
        //}
        modelCmsModel.ItemName = Utils.ChkSQL(PageRequest.GetString("ItemName"));
        if (modelCmsModel.ItemName == "")
        {
            nsb.AddErr("请输入项目名称。");
        }
        modelCmsModel.ItemUnit = Utils.ChkSQL(PageRequest.GetString("ItemUnit"));
        if (modelCmsModel.ItemUnit == "")
        {
            nsb.AddErr("请输入项目单位。");
        }
        modelCmsModel.IsUsing = PageRequest.GetString("IsUsing") == "" ? false : true;
        modelCmsModel.OrderID = PageRequest.GetInt("OrderID", -1);
        nsb.ChkErr();
        int RetFlag = bllCmsModel.Update(modelCmsModel);
        if (RetFlag == 1)
        {
            nsb.AddErr("对不起！已存在相同的内容模型。");
            nsb.ChkErr();
        }
        else
        {   
            //日志HYC
            ManageHelper.AddLog("system_modelupdate", "修改了模型：" + modelCmsModel.ModelName);
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"Modellist.aspx?sucmsg=修改成功。\";</script>");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        //删除权限hyc
        ManageHelper.CheckAdminPower("system_modeldelete");
        int ModelID = PageRequest.GetInt("ID", -1);
        if (ModelID == -1)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        bllCmsModel.Delete(ModelID);
        //日志HYC
        ManageHelper.AddLog("system_modeldelete", "删除了模型：" + ModelID);
        Response.Redirect("ModelList.aspx?sucmsg=恭喜。删除成功。");
    }
    #endregion

    #region 上升--下降
    private void ChangeOrderID()
    {
        ManageHelper.CheckAdminPower("system_modelChangeOrderID");//权限HYC
        int flag = PageRequest.GetInt("state", 0);
        int ID = PageRequest.GetInt("ID",-1);
        if (flag != 0)
        {
            bllCmsModel.ChageOrderID(flag,ID);
            //Response.Write("<script type=\"text/javascript\">location.href='Modellist.aspx'</script>");
            Response.Redirect("Modellist.aspx");
        }
        else
        {
            nsb.AddErr("操作有误。"); nsb.ChkErr();
        }
    }
    #endregion
}
